<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="card">
      <!-- 添加用户按钮 -->
      <el-button
        type="primary"
        class="butt_tian"
        @click="addDialogVisible = true"
        >添加商品</el-button
      >
      <el-table
        :data="categoriesList"
        row-key="cat_id"
        border
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="cat_name" label="商品分类"> </el-table-column>
        <el-table-column prop="cat_deleted" label="是否有效" width="80px">
          <template slot-scope="scope">
            <el-button
              size="mini"
              v-if="scope.row.cat_deleted == '0'"
              type="success"
              icon="el-icon-check"
              circle
            ></el-button>
            <el-button
              size="mini"
              v-else
              type="danger"
              icon="el-icon-close"
              circle
            ></el-button>
          </template>
        </el-table-column>
        <el-table-column prop="cat_level" label="排序" width="80px">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.cat_level == '0'" type="#">一级</el-tag>
            <el-tag v-else-if="scope.row.cat_level == '1'" type="success"
              >二级</el-tag
            >
            <el-tag v-else type="warning">三级</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="small"
              >编辑</el-button
            >
            <el-button
              size="small"
              type="danger"
              icon="el-icon-location-outline"
              @click="removeRolesById(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import {
  getCateList,
} from "@/api/categories";
export default {
  data() {
    return {
      total: 0,
      categoriesList: [],
      addDialogVisible: false,

      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
    };
  },
  created() {
    this.getCateList();
  },
  methods: {
    // 获取数据
    async getCateList() {
      const res = await getCateList(this.queryInfo);
      // console.log(res);
      if (res.meta.status !== 200) {
        return this.$message.error("获取商品分类数据失败！");
      }
      this.categoriesList = res.data.result;
      // console.log(res);
      this.total = res.data.total;
      console.log(this.categoriesList);
    },
    handleSizeChange(newSize) {
      // console.log("newSize",newSize);
      this.queryInfo.pagesize = newSize;
      this.getCateList();
    },
    handleCurrentChange(newPage) {
      // console.log("newPage",newPage);
      this.queryInfo.pagenum = newPage;
      this.getCateList();
    },
  },
};
</script>

<style>
.butt_tian {
  margin-bottom: 15px;
}
.ipn_reo {
  margin-bottom: 15px;
}
</style>